package com.core.image.tcp;

import com.core.image.core.IoContext;
import com.core.image.core.impl.IoSelectorProvider;
import com.core.image.core.impl.SchedulerImpl;
import com.core.image.utils.Foo;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * describe:
 *
 * @author 库琦
 * @date 2020/01/21
 */
public class Server {

    public static void main(String[] args)throws Exception {
        File cachePath = Foo.getCacheDir("server");

        IoContext.setup()
                .ioProvider(new IoSelectorProvider())
                .scheduler(new SchedulerImpl(1))
                .start();
        TCPServer tcpServer = new TCPServer(3344,cachePath);
        boolean flag = tcpServer.start();
        if (!flag){
            System.out.println("服务器端启动异常。。。。。。。。。。。。。");
            return;
        }
         UDPProvider.start(3344);
        write(tcpServer);


    }

    public static  void write(TCPServer client){
        try {
            InputStream in = System.in;
           do {
               BufferedReader reader = new BufferedReader(new InputStreamReader(in));
               String msg = reader.readLine();
               client.broadcast(msg);

           }while (true);


        }catch (Exception e){
            e.printStackTrace();
        }


    }
}
